import pymysql
from flask import Blueprint, render_template, request, session, current_app
from flask import redirect
from ..utils.sql import SQLHelper

account = Blueprint('account', __name__)


@account.route('/login', methods=['GET', 'POST'])
def login():

    if request.method == 'GET':
        return render_template('login.html')
    user = request.form.get('user')
    pwd = request.form.get('pwd')

    obj = SQLHelper.fetch_one("select id, name from users where name=%s and pwd=%s", [user, pwd])
    print(obj)
    if obj:
        session.permanent = True
        session['user_info'] = {'id': obj['id'], 'name': obj['name']}
        return redirect('/index2')
    else:
        return render_template('login.html', msg='用户名或密码错误')